#include "ASScrMain.h"
#include "ASScreenMgr.h"
#include "..\app\asappimginfo.h"
#include "..\control\ASAdvbar.h"
#include "ASScrCategory.h"
#include "ASScrList.h"
#include "ASScrDetail.h"
#include "mrc_advbar.h"
#include "ASWaitingScreen.h"
#include "res_str.h"
#include "ASScreenShare.h"
#include "..\app\ASAppConst.h"
#include "..\app\ASLogic.h"
#include "..\app\ASApp.h"
#include "ASConfirmScreen.h"
#include "ASScrProcess.h"
#include "ASScrSetting.h"

static as_screen_process* s_scr_process;
static process_title s_pro_title[ENUM_CATEGORY_COUNT];

#define AS_ADD_PROCESS_TITLE(title_cn) do{\
        s_pro_title[idx].img_title_cn = title_cn;\
        idx++;\
    }while(0);

static void as_scr_process_add_adv(as_screen* screen_p)
{
    as_rect area;
    area.x = 0;
    area.y = 0;
    area.w = as_get_lcd_width();
    area.h = as_app_get_advbar_height();
    as_screen_add_advbar(screen_p, area);
}

static void as_scr_process_rsk_evt(void)
{
    as_show_exit();
}

static void as_scr_process_create_softkey(as_screen* screen_p)
{
    as_screen_create_softkey_bar(screen_p, ASNULL, TEXT_EXIT, ASNULL, as_scr_process_rsk_evt);
}

static void as_scr_ascii_to_ucs2(const ASSTR src1, const ASSTR src2, ASWSTR dst1, ASWSTR dst2, ASINT dst1len, ASINT dst2len)
{
    as_memset(dst1, 0, dst1len);
    as_memset(dst2, 0, dst2len);
    as_ascii_to_ucs2(dst1, src1);
    as_ascii_to_ucs2(dst2, src2);
}

static void as_scr_process_entry_main(void)
{
    as_screen_manager* scr_mgr = as_get_screen_stack_instance();
    scr_mgr->delete_and_pop_screen();
    as_scr_main_show();
}

static void as_scr_process_entry_setting(void)
{
    as_screen_manager* scr_mgr = as_get_screen_stack_instance();
    scr_mgr->delete_and_pop_screen();
    as_scr_setting_show();
}
static void as_scr_process_tab_evt(ASINT idx)
{
    switch(idx)
    {
        case 0:
            as_scr_process_entry_main();
            break;

        case 1:
            break;

        case 2:
            as_scr_process_entry_setting();
            break;
    }
}


static void as_scr_add_head(as_screen* screen_p)
{
    as_rect area;
    ASINT w, h, strh;
    ASUINT param[6];
    ASWCHAR tmp[30], tmp2[30];
    as_tablebar* tab_p;
    as_get_internal_img_res_size(IMG_TAB_VIEW, &w, &h);
    strh = as_get_font_height();
    h = h > strh ? h : strh;
    s_scr_process->head_height = h + as_app_get_advbar_height();
    area.w = as_get_lcd_width();
    area.h = (ASINT16)h;
    area.x = 0;
    area.y = as_app_get_advbar_height();
    param[0] = 3;
    param[1] = AS_TABLEBAR_IMAGE_TYPE;
    param[2] = AS_TABLEBAR_SHOW_HORIZONTAL;
    tab_p = (as_tablebar*)screen_p->add_ctrl(screen_p, AS_CTRL_TABLEBAR, area, 3, (void**)param);
    as_scr_ascii_to_ucs2(IMG_TAB_VIEW, IMG_TAB_VIEW_D, tmp, tmp2, 30, 30);
    tab_p->add_item((as_control*)tab_p, tmp, tmp2, NULL, AS_IMAGE_FROM_RES);
    as_scr_ascii_to_ucs2(IMG_TAB_PROCESS, IMG_TAB_PROCESS_D, tmp, tmp2, 30, 30);
    tab_p->add_item((as_control*)tab_p, tmp, tmp2, NULL, AS_IMAGE_FROM_RES);
    as_scr_ascii_to_ucs2(IMG_TAB_MORE, IMG_TAB_MORE_D, tmp, tmp2, 30, 30);
    tab_p->add_item((as_control*)tab_p, tmp, tmp2, NULL, AS_IMAGE_FROM_RES);
    tab_p->set_bg_color((as_control*)tab_p, 0x0);
    tab_p->change_focus_idx((as_control*)tab_p, 1);
    tab_p->set_cb((as_control*)tab_p, as_scr_process_tab_evt);
    s_scr_process->tab = tab_p;
}

static as_progressbar* as_scr_process_add_progressbar(as_screen* screen_p, as_rect area, ASINT percent)
{
    ASINT color[1];
    ASUINT param[2];
    as_progressbar* pb;
    param[0] = AS_PB_TYPE_COLOR;
    color[0] = AS_PB_FG_COLOR;
    pb = (as_progressbar*)screen_p->add_ctrl(screen_p, AS_CTRL_PROGRESSBAR, area, 1, (void**)param);
    pb->set_bg_fill_color((as_control*)pb, AS_PB_BG_COLOR);
    pb->set_bg_frame_color((as_control*)pb, AS_PB_BG_COLOR);
    pb->set_fg_color((as_control*)pb, 1, color);
    pb->set_percent((as_control*)pb, percent);
    return pb;
}

static void as_scr_process_add_label(as_screen* screen_p, as_rect area, ASSTR str)
{
    as_label* lb;
    as_global_config* config_p = as_get_global_config();
    ASUINT param[2];
    param[0] = (ASUINT)as_ucs2_from_gb2312(str);
    lb = (as_label*)screen_p->add_ctrl(screen_p, AS_CTRL_LABEL, area, 1, (void**)param);
    lb->set_bg_hidden((as_control*)lb, ASTRUE);
    lb->set_text_color((as_control*)lb, SCREEN_SOFTKEY_COLOR);
}
static void as_scr_process_add_process(as_screen* screen_p)
{
    ASINT strw;
    ASSTR str;
    as_rect area;
    as_global_config* config_p = as_get_global_config();
    str = TEXT_NOVICE;
    strw = as_get_wstr_width(as_ucs2_from_gb2312(str));
    area.x = AS_PB_TOTAL_X;
    area.y = s_scr_process->head_height;
    area.w = strw;
    area.h = as_get_font_height();
    as_scr_process_add_label(screen_p, area, str);
    str = TEXT_MASTER;
    strw = as_get_wstr_width(as_ucs2_from_gb2312(str));
    area.x = as_get_lcd_width() - AS_PB_TOTAL_X - strw;
    area.y = s_scr_process->head_height;
    area.w = strw;
    area.h = as_get_font_height();
    as_scr_process_add_label(screen_p, area, str);
    area.x = AS_PB_TOTAL_X;
    area.y = s_scr_process->head_height + area.h;
    area.w = AS_PB_TOTAL_W;
    area.h = AS_PB_TOTAL_H;
    s_scr_process->pb_total = as_scr_process_add_progressbar(screen_p, area, as_logic_get_percent(ENUM_RECORD_ALL));
}

static void as_scr_process_title_init(void)
{
    ASINT idx = 0;
    AS_ADD_PROCESS_TITLE(IMG_T_SIXTY_CN);
    AS_ADD_PROCESS_TITLE(IMG_T_BUTTERFLY_CN);
    AS_ADD_PROCESS_TITLE(IMG_T_COWGIRL_CN);
    AS_ADD_PROCESS_TITLE(IMG_T_CUNNI_CN);
    AS_ADD_PROCESS_TITLE(IMG_T_DOGGY_CN);
    AS_ADD_PROCESS_TITLE(IMG_T_EXOTIC_CN);
    AS_ADD_PROCESS_TITLE(IMG_T_F2F_CN);
    AS_ADD_PROCESS_TITLE(IMG_T_FELL_CN);
    AS_ADD_PROCESS_TITLE(IMG_T_SPOON_CN);
}

static void as_scr_process_add_category_process(as_screen* screen_p)
{
#define Y_INTER 5
    ASINT i, start_y, color[1];
    as_rect area;
    ASSTR strTitle;
    ASUINT param[6];
    as_image* img;
    as_progressbar* pb;
    color[0] = AS_PB_FG_COLOR;
    start_y = s_scr_process->head_height + 50;

    for (i = 0; i < ENUM_CATEGORY_COUNT; i++)
    {
        area.x = 0;
        area.y = start_y + (AS_PB_CATE_H + Y_INTER) * i;
        area.w = IMG_T_SIXTY_CN_W;
        area.h = IMG_T_SIXTY_CN_H;
        strTitle = s_pro_title[i].img_title_cn;
        param[0] = AS_IMAGE_FROM_RES;
        param[1] = (ASUINT)as_ucs2_from_ascii(strTitle);
        img = (as_image*)screen_p->add_ctrl(screen_p, AS_CTRL_IMAGE, area, 3, (void**)param);
        area.x = AS_PB_CATE_X;
        area.w = AS_PB_CATE_W;
        param[0] = AS_PB_TYPE_COLOR;
        pb = (as_progressbar*)screen_p->add_ctrl(screen_p, AS_CTRL_PROGRESSBAR, area, 1, (void**)param);
        pb->set_bg_fill_color((as_control*)pb, AS_PB_BG_COLOR);
        pb->set_bg_frame_color((as_control*)pb, AS_PB_BG_COLOR);
        pb->set_fg_color((as_control*)pb, 1, color);
        pb->set_percent((as_control*)pb, as_logic_get_percent(i));
    }
}
void as_scr_process_init(as_screen* screen_p)
{
    s_scr_process = (as_screen_process*)screen_p;
    screen_p->set_bg_color(screen_p, SCREEN_BG_COLOR);
    as_scr_process_add_adv(screen_p);
    as_scr_add_head(screen_p);
    as_scr_process_add_process(screen_p);
    as_scr_process_create_softkey(screen_p);
    as_scr_process_title_init();
    as_scr_process_add_category_process(screen_p);
}

void as_scr_process_deinit(as_screen* screen_p)
{
    AS_UNUSED(screen_p);
}

void as_scr_process_show(void)
{
    as_screen_manager* scr_mgr = as_get_screen_stack_instance();
    as_screen* scr_p;
    as_ret ret;
    ret = scr_mgr->new_and_push_screen(AS_SCREEN_PROCESS);
    scr_p = scr_mgr->get_cur_screen();

    if (scr_p)
    {
        scr_p->paint_hdlr(scr_p);
        scr_p->flush_hdlr(scr_p);
    }
}